package com.css.dzj.zfjg.modules.supervisionreportinfo.service.impl;

import com.css.dzj.zfjg.common.util.R;
import com.css.dzj.zfjg.common.util.ServiceUtil;
import com.css.dzj.zfjg.modules.supervisionreportinfo.entity.JgFbJgjbEntity;
import com.css.dzj.zfjg.modules.supervisionreportinfo.entity.JgTsjbReportInfoEntity;
import com.css.dzj.zfjg.modules.supervisionreportinfo.entity.QJgTsjbReportInfoEntity;
import com.css.dzj.zfjg.modules.supervisionreportinfo.entity.QJgFbJgjbEntity;
import com.css.dzj.zfjg.modules.supervisioncompinfo.entity.QJgTsjbUserInfoEntity;
import com.css.dzj.zfjg.modules.supervisionreportinfo.mapper.JgFbJgjbMapper;
import com.css.dzj.zfjg.modules.supervisionreportinfo.mapper.JgTsjbReportInfoMapper;
import com.css.dzj.zfjg.modules.supervisionreportinfo.service.JgBlReportService;
import com.css.dzj.zfjg.modules.supervisionreportinfo.vo.ReportMsg;
import com.css.dzj.zfjg.sys.user.entity.LoginUser;
import com.querydsl.core.QueryResults;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.Projections;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class JgBlReportServiceImpl implements JgBlReportService {
    @Autowired
    private JPAQueryFactory jpaQueryFactory;
    @Autowired
    private ServiceUtil serviceUtil;
    @Autowired
    private JgFbJgjbMapper jgFbJgjbMapper;
    @Autowired
    private JgTsjbReportInfoMapper jgTsjbReportInfoMapper;
    /**
     * 办理列表查询显示
     * @param pageable
     * @param tipTerritory
     * @param tipObject
     * @param fbBureau
     * @param tipType
     * @param name
     * @param taskState
     * @param fbStartTime
     * @param fbEndTime
     * @return
     */
    @Override
    public QueryResults<ReportMsg> getReportEntity(Pageable pageable, String tipTerritory, String tipObject,
                                                   String tipType, String name, String taskState, String fbStartTime,
                                                   String fbEndTime) {
        //监管举报信息表
        QJgTsjbReportInfoEntity qEntity  = QJgTsjbReportInfoEntity.jgTsjbReportInfoEntity;
        //投诉举报用户信息表
        QJgTsjbUserInfoEntity dEntity = QJgTsjbUserInfoEntity.jgTsjbUserInfoEntity;
        //举报任务分办表
        QJgFbJgjbEntity bEntity = QJgFbJgjbEntity.jgFbJgjbEntity;
        Predicate predicate = qEntity.isNotNull().or(qEntity.isNull());
        predicate = ("".equals(tipTerritory) || tipTerritory == null) ? predicate : ExpressionUtils.and(predicate, qEntity.tipTerritory.like("%"+tipTerritory+"%"));
        predicate = ("".equals(tipObject) || tipObject == null) ? predicate : ExpressionUtils.and(predicate, qEntity.tipObject.like("%"+tipObject+"%"));
        predicate = ("".equals(tipType) || tipType == null) ? predicate : ExpressionUtils.and(predicate, qEntity.tipType.like("%"+tipType+"%"));
        predicate = ("".equals(taskState) || taskState == null) ? predicate : ExpressionUtils.and(predicate, qEntity.taskState.eq(taskState));
        //predicate = ("".equals(status) || status == null) ? predicate : ExpressionUtils.and(predicate, qEntity.status.like("%"+status+"%"));
        predicate = ("".equals(name) || name == null) ? predicate : ExpressionUtils.and(predicate, dEntity.name.like("%"+name+"%"));

        //分办时间
        if((StringUtils.isEmpty(fbStartTime)) && (!StringUtils.isEmpty(fbEndTime))) {
            Date endDate = null;
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
            try {
                endDate = sf.parse(fbEndTime +" 23:59:59.999");
            } catch (ParseException e) {
                e.printStackTrace();
            }
            predicate = ExpressionUtils.and(predicate, qEntity.fbTime.loe(endDate));
        }
        if ((!StringUtils.isEmpty(fbStartTime)) && (StringUtils.isEmpty(fbEndTime))) {
            Date startDate = null;
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
            try {
                startDate = sf.parse(fbStartTime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            predicate = ExpressionUtils.and(predicate, qEntity.fbTime.goe(startDate));
        }
        if ((!StringUtils.isEmpty(fbStartTime)) && (!StringUtils.isEmpty(fbEndTime))) {
            Date startDate = null;
            Date endDate = null;
            SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
            try {
                startDate = sf.parse(fbStartTime+" 00:00:00.000");
                endDate = sf.parse(fbEndTime + " "+"23:59:59.999");
            } catch (ParseException e) {
                e.printStackTrace();
            }
            predicate = ExpressionUtils.and(predicate, qEntity.fbTime.between(startDate, endDate));
        }

        return jpaQueryFactory
                .select(
                        Projections.bean(ReportMsg.class,
                                qEntity.registerCode,qEntity.tipTerritory,qEntity.tipObject,bEntity.fbBureau,bEntity.fbDepartment,qEntity.tipType,dEntity.name,qEntity.fbTime,qEntity.taskState))
                .from(qEntity)
                .leftJoin(dEntity).on(qEntity.registerCode.eq(dEntity.registerCode))
                .leftJoin(bEntity).on(qEntity.registerCode.eq(qEntity.registerCode))
                .where(predicate)
                .orderBy(qEntity.createTime.desc())
                .offset(pageable.getOffset())
                .limit(pageable.getPageSize())
                .fetchResults();
    }


    /**
     * 查收/拒绝监管举报办理信息
     * @param jgFbJgjbEntity
     */
    @Transactional
    @Override
    public void update(JgFbJgjbEntity jgFbJgjbEntity, HttpServletRequest request, String flag) {
        try {
            R r = serviceUtil.getUserInfoByRequest(request);
            if(r.getCode().equals("200")) {
                LoginUser userInfo =  (LoginUser)r.get("data");
                jgFbJgjbEntity.setCreateUser(userInfo.getLoginName());
            }
            jgFbJgjbEntity.setFbState(flag);
            if(flag.equals("103")) {
                jgFbJgjbEntity.setDelFlag("2");
                jgFbJgjbEntity.setRefuseReason(jgFbJgjbEntity.getRefuseReason());
            }else if(flag.equals("104")){
                //当状态为104时表示答复，记录答复时间
                jgFbJgjbEntity.setReplyTime(new Date());
            }
            //无论查收拒绝 都记录修改时间
           jgFbJgjbEntity.setUpdateTime(new Date());
            jgFbJgjbMapper.save(jgFbJgjbEntity);
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 修改监管投诉主表信息
     * @param jgTsjbReportInfoEntity
     */
    @Transactional
    @Override
    public void updatetsjb(JgTsjbReportInfoEntity jgTsjbReportInfoEntity, HttpServletRequest request, String flag) {
        try{
            R r = serviceUtil.getUserInfoByRequest(request);
            if(r.getCode().equals("200")) {
                LoginUser userInfo =  (LoginUser)r.get("data");
                jgTsjbReportInfoEntity.setUpdateUser(userInfo.getLoginName());
            }
            if(StringUtils.isEmpty(jgTsjbReportInfoEntity.getFbTime())){
                jgTsjbReportInfoEntity.setFbTime(new Date());
            }
            jgTsjbReportInfoEntity.setTaskState(flag);
            jgTsjbReportInfoEntity.setUpdateTime(new Date());
            jgTsjbReportInfoMapper.save(jgTsjbReportInfoEntity);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
